//
//  QLToast.m
//  qibla
//
//  Created by mac on 2024/11/28.
//

#import "QLToast.h"
@interface QLToast ()

@property (nonatomic, assign) QLToastType toastType;
@property (nonatomic, assign) QLToastPosition toastPosition;
@property (nonatomic, assign) NSInteger duration;

@end

@implementation QLToast

+ (void)makeToast:(NSString *)msg toView:(UIView *)view
{
    [self makeToast:msg toastType:QLToastTypeNormal toastPosition:QLToastPositionBottom duration:1 toView:view];
}
+ (void)makeToast:(NSString *)msg toastType:(QLToastType)toastType toView:(UIView *)view
{
    switch (toastType) {
        case QLToastTypeNormal:
        case QLToastTypeInfo:
        case QLToastTypeSuccess:
            [self makeToast:msg toastType:toastType toastPosition:QLToastPositionBottom duration:1 toView:view];
            break;
        case QLToastTypeWarning:
        case QLToastTypeError:
            [self makeToast:msg toastType:toastType toastPosition:QLToastPositionBottom duration:2 toView:view];
            break;
        default:
            break;
    }
}
+ (void)makeToast:(NSString *)msg toastType:(QLToastType)toastType toastPosition:(QLToastPosition)toastPosition toView:(UIView *)view
{
    switch (toastType) {
        case QLToastTypeNormal:
        case QLToastTypeInfo:
        case QLToastTypeSuccess:
            [self makeToast:msg toastType:toastType toastPosition:toastPosition duration:1 toView:view];
            break;
        case QLToastTypeWarning:
        case QLToastTypeError:
            [self makeToast:msg toastType:toastType toastPosition:toastPosition duration:2 toView:view];
            break;
        default:
            break;
    }
}
+ (void)makeToast:(NSString *)msg toastType:(QLToastType)toastType toastPosition:(QLToastPosition)toastPosition duration:(NSInteger)duration toView:(UIView *)view
{
    if (msg.length == 0) {
        return;
    }
    CGFloat viewWidth = view.bounds.size.width;
    CGFloat viewHeight = view.bounds.size.height;
    
    CGFloat msgHeight = [CommondHandle getHeightLineWithString:msg withWidth:viewWidth-80 withFont:[UIFont pingFangSCMediumWithSize:18] minLineHeight:24];
    if (msgHeight > 48) {
        msgHeight = 48;
    }
    CGFloat height = msgHeight + 64;
    CGFloat toastY = 0;
    switch (toastPosition) {
        case QLToastPositionTop:
            toastY = 64;
            break;
        case QLToastPositionCenter:
            toastY = viewHeight/2-height/2;
            break;
        case QLToastPositionBottom:
            toastY = viewHeight-height-64;
            break;
        default:
            break;
    }
    UIColor *bgColor = myRGB(0, 67, 46);
    switch (toastType) {
        case QLToastTypeInfo:
            bgColor = myRGB(0, 80, 143);
            break;
        case QLToastTypeSuccess:
            bgColor = myRGB(36, 119, 53);
            break;
        case QLToastTypeWarning:
            bgColor = myRGB(144, 101, 8);
            break;
        case QLToastTypeError:
            bgColor = myRGB(153, 24, 9);
            break;
        default:
            bgColor = myRGB(0, 67, 46);
            break;
    }
    
    QLToast *toast = [[self alloc] initWithFrame:CGRectMake(24, toastY, viewWidth-48, height)];
    toast.backgroundColor = [bgColor colorWithAlphaComponent:0.64];
    toast.alpha = 0;
    [view addSubview:toast];
    [CommondHandle cornerTheView:toast byCorner:isRTL ? (UIRectCornerTopLeft | UIRectCornerBottomRight) : (UIRectCornerTopRight | UIRectCornerBottomLeft) withRadias:40];

    UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 32, viewWidth-80, msgHeight)];
    msgLabel.numberOfLines = 2;
    msgLabel.attributedText = [CommondHandle changeStrToCustomLineHeightAttributedStr:msg lineHeight:24];
    msgLabel.textAlignment = NSTextAlignmentCenter;
    msgLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    msgLabel.font = [UIFont pingFangSCMediumWithSize:16];
    msgLabel.textColor = [UIColor whiteColor];
    [toast addSubview:msgLabel];
    
    [UIView animateWithDuration:0.25 animations:^{
        toast.alpha = 1;
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            toast.alpha = 0;
        } completion:^(BOOL finished) {
            [toast removeFromSuperview];
        }];
    });
    
}


@end
